<?php
include '../../system/data/settings.php';
include '../../system/libraries/database.php';
include 'stats.inc.php';

$action = $_REQUEST['action'];
if ($action == 'get-data')
{
if (empty($_GET['datefrom']))
  $dateFrom = date("Y-m-d").' 00:00:00';
  //$dateFrom = '2011-10-02';
else
  $dateFrom = $_GET["datefrom"];

  
if (empty($_GET['dateto']))
  $dateTo = date("Y-m-d").' 23:59:59';
  //$dateTo = '2011-10-15';

else
  $dateTo= $_GET["dateto"];

if (empty($_GET['categoryId']))
  $categoryID = 13;
else
  $categoryID= $_GET["categoryId"];


if (empty($_GET['timeunit']))
  $timeUnit = 0;
else
  $timeUnit= $_GET["timeunit"];

   buildData($dateFrom, $dateTo, $timeUnit,$categoryID);
}
else if ($action == 'get-categories')
{
  if (empty($_GET['datefrom']))
  $dateFrom = date("Y-m-d").' 00:00:00';
  //$dateFrom = '2011-10-02';
else
  $dateFrom = $_GET["datefrom"];

  
if (empty($_GET['dateto']))
  $dateTo = date("Y-m-d").' 23:59:59';
  //$dateTo = '2011-10-15';

else
  {
  $dateTo= $_GET["dateto"];

  $categories = array();
  $columns = "categories.name AS `category_name`, categories.id as `category_id`, SUM(item_daily_views.views) AS nr_views";
  $from = "item_daily_views INNER JOIN items ON item_daily_views.item_id = items.id
  INNER JOIN categories ON items.category_id = categories.id";
  
  $where = "item_daily_views.day_stamp>=(TO_DAYS('$dateFrom')-719528) AND 
  item_daily_views.day_stamp<=(TO_DAYS('$dateTo')-719528)  AND categories.status = 1";
  
  $query = "SELECT $columns FROM $from WHERE $where GROUP BY `category_name` ORDER BY `nr_views` DESC";
  $categories = ExecuteQuery($query); 
  
  foreach ($categories as $key => $value)
     {
       
      $response[$value['category_name']] = array('category_name'=> $value['category_name'], 'ID'=>$value['category_id'] );
     }
     
  print json_encode($response);
  
  }

}
    

function GetProductsID($dateFrom, $dateTo, $categoryID)
{
  $categories = array();
  $columns = " item_daily_views.item_id AS item_ID, items.title_ro AS `item_name`, SUM( item_daily_views.views ) AS nr_views";
  $from = "item_daily_views INNER JOIN items ON item_daily_views.item_id = items.id
  INNER JOIN categories ON items.category_id = categories.id";
  
  $where = "item_daily_views.day_stamp>=(TO_DAYS('$dateFrom')-719528) AND 
  item_daily_views.day_stamp<=(TO_DAYS('$dateTo')-719528) AND categories.id = $categoryID";
  
  $query = "SELECT $columns FROM $from WHERE $where GROUP BY item_name ORDER BY `nr_views` DESC LIMIT 10";
  $categories = ExecuteQuery($query); 
  return $categories;
}

function GetData($dateFrom, $dateTo, $timeUnit, $itemID)
{
  $results = array();

  $TOTAL = 0;
  
  $dataMinim = array('value'=> 20000);
  $dataMaxim = array('value'=> 0 );
  
  $columns = " item_daily_views.day_stamp AS `group_column` , items.title_ro AS `item_name` , SUM( item_daily_views.views ) AS `total`";
  
  $where = "item_daily_views.day_stamp>=(TO_DAYS('$dateFrom')-719528) AND 
  item_daily_views.day_stamp<=(TO_DAYS('$dateTo')-719528) AND
  item_daily_views.item_id =  $itemID";
  
  $from = "item_daily_views INNER JOIN items ON item_daily_views.item_id = items.id
  INNER JOIN categories ON items.category_id = categories.id";
  
  $suffix = 'GROUP BY `group_column`';
  $query = "SELECT $columns FROM $from  WHERE $where $suffix";
  $rows = ExecuteQuery($query);
  $curTimeRangeStart = $dateFrom;
  $curTimeRangeEnd = GetTimeRangeEnd($curTimeRangeStart, $timeUnit);
  $curTotalValue = 0;

  foreach ($rows as $key => $value)
  {
    $day = date("Y-m-d", $value['group_column']* 86400);
    
    while ($day > $curTimeRangeEnd)
    {
      $dataMaxim =  Maxim($curTotalValue, $curTimeRangeStart, $curTimeRangeEnd, $dataMaxim, $timeUnit);
      $dataMinim =  Minim($curTotalValue, $curTimeRangeStart, $curTimeRangeEnd, $dataMinim, $timeUnit);
      $results[] = array('range-start' => $curTimeRangeStart, 'range-end' => $curTimeRangeEnd, 'value' => $curTotalValue);
      
      $curTotalValue = 0;
      $curTimeRangeStart = GetNextTimeRangeStart($curTimeRangeStart, $timeUnit);
      $curTimeRangeEnd = GetTimeRangeEnd($curTimeRangeStart, $timeUnit);
    }
    $curTotalValue += $value['total'];

    $TOTAL += $value['total']; 
 
  }

  $endDay = $dateTo;

  while ($endDay > $curTimeRangeEnd)
  {

    $results[] = array('range-start' => $curTimeRangeStart, 'range-end' => $curTimeRangeEnd, 'value' => $curTotalValue);
    $curTotalValue = 0;
    $curTimeRangeStart = GetNextTimeRangeStart($curTimeRangeStart, $timeUnit);
    $curTimeRangeEnd = GetTimeRangeEnd($curTimeRangeStart, $timeUnit);
  }
  
  $dataMaxim =  Maxim($curTotalValue, $curTimeRangeStart, $curTimeRangeEnd, $dataMaxim, $timeUnit);
  $dataMinim =  Minim($curTotalValue, $curTimeRangeStart, $curTimeRangeEnd, $dataMinim, $timeUnit);
  
  $results[] = array('range-start' => $curTimeRangeStart, 'range-end' => $endDay, 'value' => $curTotalValue);
  $results['data'] = array('Min' =>$dataMinim, 'Max' =>$dataMaxim, 'Total' => $TOTAL);
  
  return $results; 
}

function buildData($dateFrom, $dateTo, $timeunit, $categoryID)
{
  $response = array();
  $categories = GetProductsID($dateFrom, $dateTo, $categoryID);
       
  foreach ($categories as $key => $value)
    {
      $response[$value['item_name']] = GetData($dateFrom, $dateTo, $timeunit, $value['item_ID'] );
    }
    print json_encode($response);
}


?>
